import { requestClient } from '#/api/request';
import type { ApiResponse } from '#/api/types';

export namespace SystemDeptApi {
  export interface SystemDept {
    [key: string]: any;
    children?: SystemDept[];
    id: string;
    name: string;
    remark?: string;
    status: 0 | 1;
    createdTime?: string;
    parentId?: string;
    code?: string;
  }
}

/**
 * 获取部门列表数据（树形结构）
 */
async function getDeptTree() {
  return requestClient.get<ApiResponse<Array<SystemDeptApi.SystemDept>>>(
    '/depts/tree',
  );
}

/**
 * 获取部门列表数据（扁平结构）
 */
async function getDeptList() {
  return requestClient.get<ApiResponse<Array<SystemDeptApi.SystemDept>>>(
    '/depts/list',
  );
}

/**
 * 创建部门
 * @param data 部门数据
 */
async function createDept(
  data: Omit<SystemDeptApi.SystemDept, 'children' | 'id'>,
) {
  return requestClient.post<ApiResponse<string>>(
    '/depts',
    data
  );
}

/**
 * 更新部门
 *
 * @param id 部门 ID
 * @param data 部门数据
 */
async function updateDept(
  id: string,
  data: Omit<SystemDeptApi.SystemDept, 'children' | 'id'>,
) {
  return requestClient.put<ApiResponse<null>>(
    `/depts/${id}`,
    data
  );
}

/**
 * 删除部门
 * @param id 部门 ID
 */
async function deleteDept(id: string) {
  return requestClient.delete<ApiResponse<null>>(
    `/depts/${id}`
  );
}

/**
 * 获取部门下拉选择数据
 */
async function getDeptSelects() {
  return requestClient.get<ApiResponse<Array<{
    value: string;
    label: string;
    disabled: boolean;
    parentId: string;
  }>>>(
    '/depts/select'
  );
}

export { createDept, deleteDept, getDeptList, getDeptTree, updateDept, getDeptSelects };
